home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / tcpdump-3.0 / tcpcomp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-17  |  2.1 KB  |  109 lines

  1. /* A stripped-down version of tcpdump that accepts a filter expression
  2.  * and outputs its binary program in network byte order.
  3.  * Length comes first and then an array of BPF instructions.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #ifdef __STDC__
  10. #include <stdlib.h>
  11. #endif
  12. #include <unistd.h>
  13.  
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16.  
  17. #include "interface.h"
  18.  
  19. #include <order.h>
  20. #include <pcap.h>
  21.  
  22. #include "fddi.h"
  23. int        fddipad = FDDIPAD;    /* yek, keep gencode.o references happy */
  24.  
  25. int tflag = 1;                    /* yek, keep util.o references happy */
  26.  
  27. char *program_name;
  28. int thiszone;
  29.  
  30. extern void bpf_dump(struct bpf_program *, int);
  31.  
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.     extern char *optarg;
  36.     extern int optind, opterr;
  37.  
  38.     int op, i, len;
  39.     int dflag = 0, Oflag = 1;
  40.     int linktype = DLT_EN10MB, snaplen = DEFAULT_SNAPLEN;
  41.     u_long netmask = 0;
  42.     char *cp, *cmdbuf;
  43.     struct bpf_program fcode;
  44.  
  45.     if ((cp = strrchr(argv[0], '/')) != NULL)
  46.         program_name = cp + 1;
  47.     else
  48.         program_name = argv[0];
  49.  
  50.     opterr = 0;
  51.     while ((op = getopt(argc, argv, "di:m:Os:")) != EOF)
  52.         switch (op) {
  53.         case 'd':
  54.             ++dflag;
  55.             break;
  56.         case 'i':
  57.             linktype = atoi(optarg);
  58.             break;
  59.         case 'm':
  60.             netmask = inet_addr(optarg);
  61.             break;
  62.         case 'O':
  63.             Oflag = 0;
  64.             break;
  65.         case 's':
  66.             snaplen = atoi(optarg);
  67.             break;
  68.         default:
  69.             usage();
  70.         }
  71.  
  72.     cmdbuf = copy_argv(&argv[optind]);
  73.  
  74.     if (pcap_compile_offline(cmdbuf, &fcode, Oflag, linktype, netmask, snaplen) < 0)
  75.     {
  76.         fprintf(stderr, "error compiling filter expression\n");
  77.         exit(1);
  78.     }
  79.  
  80.     if (dflag) {
  81.         bpf_dump(&fcode, dflag);
  82.         return 0;
  83.     }
  84.  
  85.     len = fcode.bf_len;            /* save in case we are going to swap */
  86.     if (byteorder() != BIG_ENDIAN)
  87.     {
  88.         for (i = 0; i < fcode.bf_len; i++)
  89.         {
  90.             SWAP_INT16(fcode.bf_insns[i].code);
  91.             SWAP_INT32(fcode.bf_insns[i].k);
  92.         }
  93.         SWAP_INT32(fcode.bf_len);
  94.     }
  95.  
  96.     fwrite(&fcode.bf_len, sizeof(fcode.bf_len), 1, stdout);
  97.     for (i = 0; i < len; i++)
  98.         fwrite(&fcode.bf_insns[i], sizeof(fcode.bf_insns[i]), 1, stdout);
  99.  
  100.     return 0;
  101. }
  102.  
  103. void usage()
  104. {
  105.     fprintf(stderr,
  106. "usage: compile [-dO] [-i interface] [-m netmask] [-s snaplen] [expr]\n");
  107.     exit(1);
  108. }
  109.